package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;


/**
 * The Class ImageSerializer.
 * this class converts an image file to a serialized 64bit string.
 * @author Tomer Elkayam
 * 
 */
public class ImageSerializer 
{	
	
	/** The file. */
	private File file;
	
	/** The in file path. */
	private String inFilePath;
	
	/** The out file path. */
	private String outFilePath;
	
	/** The image data string. */
	private String imageDataString;
	
	/**
	 * Instantiates a new image serializer.
	 * <br>
	 * THIS METHOD SOULD BE USED ONLY BY SERVER SIDE.
	 * @param inputFilePath the input file path
	 */
	public ImageSerializer(String inputFilePath)
	{
		//i.e "/Users/jeeva/Pictures/wallpapers/water-drop.jpg");
		this.inFilePath = inputFilePath;
		file = new File(inFilePath);
	}
	
	/**
	 * Instantiates a new image serializer.
	 *<br>
	 *THIS METHOD IS USED ONLY BY CLIENT SIDE.
	 * @param outFilePath the out file path
	 * @param imageDataString the image data string
	 */
	public ImageSerializer(String outFilePath,String imageDataString)
	{
		this.outFilePath = outFilePath;
		this.imageDataString =imageDataString;
	}
	
	/**
	 * Serialize photo.
	 * <br>
	 * converts the photo to a string.
	 * @return the string representation of a photo file
	 */
	public String serializePhoto()
	{
		try 
		{
			// Reading a Image file from file system
			FileInputStream imageInFile = new FileInputStream(file);
			byte imageData[] = new byte[(int) file.length()];
			imageInFile.read(imageData);
			
			// Converting Image byte array into Base64 String
			String imageDataString = encodeImage(imageData);
			imageInFile.close();
			return imageDataString;
		}
		
		catch (FileNotFoundException e) 
	 	{
		 System.out.println("Image not found" + e);
		 return null;
	 	} 
		catch (IOException ioe) 
		{
			System.out.println("Exception while reading the Image ");
			ioe.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Deserialize photo.
	 * <br>
	 * converts the photo string to a file output stream
	 *
	 * @return the file output stream
	 */
	public void deserializePhoto()
	{
		try
		{
			// Converting a Base64 String into Image byte array
			byte[] imageByteArray = decodeImage(imageDataString);
			// Write a image byte array into file system
			//i.e "/Users/jeeva/Pictures/wallpapers/water-drop-after-convert.jpg"
			FileOutputStream imageOutFile = new FileOutputStream(outFilePath);
			imageOutFile.write(imageByteArray);
			imageOutFile.close();
			
			System.out.println("Image Successfully Manipulated!");
			return;
		
		}
		catch (FileNotFoundException e) 
	 	{
			System.out.println("Image not found" + e);
			return;
	 	} 
		catch (IOException ioe) 
		{
			System.out.println("Exception while reading the Image ");
			ioe.printStackTrace();
			return;
		}
		
	}
	
	/**
	 * Encodes the byte array into base64 string.
	 *
	 * @param imageByteArray - byte array
	 * @return String a {@link java.lang.String}
	 */
	
	 public static String encodeImage(byte[] imageByteArray) 
	 {
		 return Base64.encodeBase64URLSafeString(imageByteArray);
	 }

	/**
	 * Decodes the base64 string into byte array.
	 *
	 * @param imageDataString the image data string
	 * @return byte array
	 */
 	public static byte[] decodeImage(String imageDataString) 
 	{
	 	return Base64.decodeBase64(imageDataString);
	}
}
